$package('YiYa.car');
YiYa.merchant = function(){
	var _box = null;
	var _this = {
		config:{
			dataGrid:{
				title:'车辆加油记录列表',
				url:'dataList.do',
				columns:[[
				          {field:'id',checkbox:true},
				          {field:'number',title:'车牌号',width:80},
				          {field:'refuel_date',title:'加油日期',width:80,sortable:true,
				        	  formatter:function(value,row,index){
				        		  if(row["refuelDate"]!=null){
				        		  return new Date(row["refuelDate"]).format("yyyy-MM-dd");
				        		  }
						  }},
						  {field:'place',title:'所属地区',width:80},
						  {field:'lastRefuelLiter',title:'上次加油升数',width:80},
				          {field:'refuelLiter',title:'加油升数',width:80},
				          {field:'lastMileage',title:'上次公里数',width:80},
				          {field:'currentMileage',title:'公里数',width:80},
				          {field:'amount',title:'加油金额',width:80},
				          {field:'balance',title:'余额',width:80},
				          {field:'fuelConsumption',title:'百公里耗油量',width:80},
				          {field:'handledBy',title:'负责人',width:80},
				          {field:'create_time',title:'创建时间',width:120,sortable:true,
				        	  formatter:function(value,row,index){
				        		  return row["createTime"];
						  }},
				          {field:'update_time',title:'更新时间',width:120,sortable:true,
				        	  formatter:function(value,row,index){
				        		  return row["updateTime"];
						  }},
						  {field:'remark',title:'备注',width:120,
							  formatter:function(value,row,index){
								  if(value!=undefined){
									  if(value.length>10){
										  var str = value.substring(0,8);
										  return str.concat('......');	  
									  }
									  return value;
								  }
								  
						  }}
				          ]],
				          toolbar:[
				                   {id:'btnadd',text:'添加',btnType:'add'},
				                   {id:'btnedit',text:'编辑',btnType:'edit'},
				                   {id:'btndelete',text:'删除',btnType:'remove'},
				                   {id:'btnview',text:'查看',btnType:'view'}
				                   ]
			}
		},
		init:function(){
			$('.easyui-datebox').datebox({
			    formatter: function(date){ return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();},
			    parser: function(date){ return new Date(Date.parse(date.replace(/-/g,"/")));}
			});
			$("#export-btn").click(function(){
				var url = "../car/export_car.do"
				document.searchForm.action = url;
				document.searchForm.submit();
			});
			
			$("#lastRefuelLiter").change(function(){
				calcFuelConsumption();
			});
			$("#lastMileage").change(function(){
				calcFuelConsumption();
			});
			$("#currentMileage").change(function(){
				calcFuelConsumption();
			});
			//计算百公里耗油量
			function calcFuelConsumption(){
				var lastRefuelLiter = $("#lastRefuelLiter").val();
				var lastMileage = $("#lastMileage").val();
				var currentMileage = $("#currentMileage").val();
				if(lastRefuelLiter!=''&&lastMileage!=''&&currentMileage!=''){
					var fuelConsumption = (lastRefuelLiter*100/(currentMileage - lastMileage)).toFixed(2);
					$("#fuelConsumption").val(parseFloat(fuelConsumption));
		    		$("input[name='fuelConsumption']").val(fuelConsumption);
				}
			}
			_box = new YDataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function(){
	YiYa.merchant.init();
});


